#!/bin/sh
#
# radiusd Start/Stop the FreeRADIUS daemon
#
# chkconfig: - 88 10
# description: Extensible, configurable, high performance RADIUS server.

### BEGIN INIT INFO
# Provides: radiusd
# Required-Start: $network
# Required-Stop:
# Default-Start:
# Default-Stop:
# Should-Start: $time $syslog mysql ldap postgresql samba krb5-kdc
# Should-Stop:
# Short-Description: FreeRADIUS server
# Description: Extensible, configurable, high performance RADIUS server.
### END INIT INFO

# Source function library.

# Get the wrappers for the standard lsb init functions
. /lib/lsb/init-functions

# and the RHEL specific ones
. /etc/init.d/functions

prog=radiusd

[ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog

exec=${exec:=/usr/sbin/$prog}
config_dir=${config_dir:=/etc/raddb}
config=${config:=$config_dir/radiusd.conf}
pidfile=${pidfile:=/var/run/$prog/$prog.pid}
lockfile=${lockfile:=/var/lock/subsys/radiusd}

configtest() {
    echo -n $"Checking $prog configuration: "
    out=`$exec -Cxl stdout -d $config_dir`; retval=$?
    out=`echo "${out}" | tail -n 1 | sed 's/^\s*ERROR:\s*\(.*\)\s*$/\1/'`

    # Seems some LSB function implementations *really* need
    # a log message < 60 chars long, else output gets mangled.
    if [ $retval -eq 0 ]; then
        log_success_msg
    else
        if [ $(expr length "$out") -gt 60 ]; then
            log_failure_msg
            echo "$out" 1>&2
        else
            log_failure_msg "$out"
        fi
    fi
    return $retval
}

start() {
    echo -n $"Starting $prog: "

    if [ ! -x $exec ]; then
        log_failure_msg "$exec not found or not executable"
        exit 5
    fi

    if [ ! -f $config ]; then
        log_failure_msg "Can't find radiusd.conf"
        exit 6
    fi

    start_daemon -p $pidfile $exec -d $config_dir
    retval=$?
    if [ $retval -eq 0 ]; then
        log_success_msg
    else
        log_failure_msg
    fi
    return $retval
}

stop() {
    echo -n $"Stopping $prog: "
    killproc -p $pidfile $prog
    retval=$?
    if [ $retval -eq 0 ]; then
        log_success_msg
        rm -f $lockfile
    else
        log_failure_msg
    fi
    return $retval
}

restart() {
    stop
    start
}

reload() {
    # radiusd may not be capable of a 100% configuration reload depending
    # on which loadable modules are in use, if sending the server a
    # HUP is not sufficient then use restart here instead. However, we
    # prefer by default to use HUP since it's what is usually desired.
    #
    # restart

    kill -HUP `pidofproc -p $pidfile $prog`
}

force_reload() {
    restart
}

rh_status() {
    # run checks to determine if the service is running or use generic status
    status -p $pidfile $prog
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}

case "$1" in
    start)
        rh_status_q && exit 0
        configtest || exit 150
        $1
        ;;

    stop)
        rh_status_q || exit 0
        $1
        ;;

    restart)
        configtest || exit 150
        $1
        ;;

    reload)
        rh_status_q || exit 7
        configtest || exit 150
        $1
        ;;

    force-reload)
        configtest || exit 150
        force_reload
        ;;

    condrestart|try-restart)
        configtest || exit 150
        rh_status_q || exit 0
        restart
        ;;

    configtest|testconfig)
        configtest || exit 150
        ;;

    debug)
        echo -n $"Debugging $prog: "
        if rh_status_q; then
            log_failure_msg "$prog already running; for live debugging see raddebug(8)"
            exit 151
        else
            log_success_msg
        fi
        $exec -X -d $config_dir || exit $?
        ;;

    debug-threaded)
        echo -n $"Debugging $prog: "
        if rh_status_q; then
            log_failure_msg "$prog already running; for live debugging see raddebug(8)"
            exit 151
        else
            log_success_msg
        fi
        $exec -f -xx -l stdout -d $config_dir || exit $?
        ;;

    status)
        rh_status
        ;;

    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest|debug|debug-threaded}"
        exit 2
esac
exit $?
